/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UltimatumGameComponent.java
 *
 * Created on 15/Dez/2011, 17:39:57
 */

package intopo.game.impl.ultimatum;

import intopo.game.gui.AbstractGameEditorComponent;
import java.awt.BorderLayout;
import java.awt.CardLayout;

/**
 *
 * @author <a href="mailto:mariano.pedro@gmail.com">Pedro Mariano</a>
 *
 * @version 1.0 15/Dez/2011
 */
public class UltimatumGameComponent
	extends AbstractGameEditorComponent<UltimatumGame, UltimatumStrategy>
{
	private int[] error;
	/** Creates new form BeanForm */
	public UltimatumGameComponent ()
	{
		this.game = new UltimatumGame (UltimatumGame.Variant.SINGLE_GAME, 0.5f, 10);
		this.error = new int [UltimatumGame.Variant.values ().length];
		java.util.Arrays.fill (this.error, 0);
		initComponents();
		this.add (this.infoPanel, BorderLayout.SOUTH);
		this.updateInfoPanel (true);
	}


	@Override
	public String getConfigFileDescription () {
		throw new UnsupportedOperationException ("Not supported yet.");
	}

	@Override
	public UltimatumGame getGame ()
	{
		int e = this.error [this.variantComboBox.getSelectedIndex ()];
		if (e == 0) {
			UltimatumGame.Variant variant = UltimatumGame.Variant.values () [this.variantComboBox.getSelectedIndex ()];
			float probabilityFirstPlayerActProponent;
			int cakeSize;
			switch (variant) {
			case SINGLE_GAME:
				probabilityFirstPlayerActProponent = (Float) this.probabilityFirstPlayerActProponentFormattedTextField.getValue ();
				break;
			case SIMULTANEOUS_GAMES:
				probabilityFirstPlayerActProponent = -1f;
				break;
			default:
				throw new UnsupportedOperationException ("Variant not supported yet: " + variant);
			}
			cakeSize = (Integer) this.cakeSizeFormattedTextField.getValue ();
			return new UltimatumGame (variant, probabilityFirstPlayerActProponent, cakeSize);
		}
		else {
			return null;
		}
	}

	@Override
	public void setGame (UltimatumGame game)
	{
		UltimatumGame.Variant variant = game.getVariant ();
		this.variantComboBox.setSelectedIndex (variant.ordinal ());
		switch (variant) {
		case SINGLE_GAME:
			this.probabilityFirstPlayerActProponentFormattedTextField.setValue (new Float (game.getProbabilityFirstPlayerActProponent ()));
			break;
		case SIMULTANEOUS_GAMES:
			break;
		default:
			throw new UnsupportedOperationException ("Variant not supported yet: " + variant);
		}
		this.cakeSizeFormattedTextField.setValue (new Integer (game.getCakeSize ()));
	}


	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    @SuppressWarnings("unchecked")
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {
      java.awt.GridBagConstraints gridBagConstraints;

      commonParametersPanel = new javax.swing.JPanel();
      javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
      variantComboBox = new javax.swing.JComboBox (UltimatumGame.Variant.values ());
      javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
      cakeSizeFormattedTextField = new javax.swing.JFormattedTextField();
      variantPanel = new javax.swing.JPanel();
      singlePanel = new javax.swing.JPanel();
      javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
      probabilityFirstPlayerActProponentFormattedTextField = new javax.swing.JFormattedTextField();
      simultaneousPanel = new javax.swing.JPanel();

      setBorder(javax.swing.BorderFactory.createTitledBorder("game parameters"));
      setLayout(new java.awt.BorderLayout());

      commonParametersPanel.setLayout(new java.awt.GridBagLayout());

      jLabel1.setText("Variant: ");
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
      commonParametersPanel.add(jLabel1, gridBagConstraints);

      variantComboBox.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            variantComboBoxActionPerformed(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
      commonParametersPanel.add(variantComboBox, gridBagConstraints);

      jLabel2.setText("Cake size: ");
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
      commonParametersPanel.add(jLabel2, gridBagConstraints);

      cakeSizeFormattedTextField.setColumns(5);
      cakeSizeFormattedTextField.setValue(new Integer (this.game.getCakeSize ()));
      cakeSizeFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            cakeSizeFormattedTextFieldPropertyChange(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
      commonParametersPanel.add(cakeSizeFormattedTextField, gridBagConstraints);

      add(commonParametersPanel, java.awt.BorderLayout.NORTH);

      variantPanel.setLayout(new java.awt.CardLayout());

      singlePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("single variant parameters"));
      singlePanel.setLayout(new java.awt.GridBagLayout());

      jLabel3.setText("Probability first player propose: ");
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
      singlePanel.add(jLabel3, gridBagConstraints);

      probabilityFirstPlayerActProponentFormattedTextField.setColumns(5);
      probabilityFirstPlayerActProponentFormattedTextField.setValue(new Float (0.5));
      probabilityFirstPlayerActProponentFormattedTextField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
         public void propertyChange(java.beans.PropertyChangeEvent evt) {
            probabilityFirstPlayerActProponentFormattedTextFieldPropertyChange(evt);
         }
      });
      gridBagConstraints = new java.awt.GridBagConstraints();
      gridBagConstraints.gridx = 1;
      gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
      singlePanel.add(probabilityFirstPlayerActProponentFormattedTextField, gridBagConstraints);

      variantPanel.add(singlePanel, "single");
      variantPanel.add(simultaneousPanel, "simultaneous");

      add(variantPanel, java.awt.BorderLayout.CENTER);
   }// </editor-fold>//GEN-END:initComponents

	 private void cakeSizeFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_cakeSizeFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			final int value = (Integer) this.cakeSizeFormattedTextField.getValue ();
			final boolean ok = value > 1;
			this.update (this.cakeSizeFormattedTextField, ok);
			if (ok) {
				this.error [UltimatumGame.Variant.SIMULTANEOUS_GAMES.ordinal ()] &= ~1;
				this.error [UltimatumGame.Variant.SINGLE_GAME.ordinal ()] &= ~1;
				this.updateInfoPanel (true);
			}
			else {
				this.error [UltimatumGame.Variant.SIMULTANEOUS_GAMES.ordinal ()] |= ~1;
				this.error [UltimatumGame.Variant.SINGLE_GAME.ordinal ()] |= 1;
			}
		}
}//GEN-LAST:event_cakeSizeFormattedTextFieldPropertyChange

	 private void variantComboBoxActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_variantComboBoxActionPerformed
		CardLayout layout = (CardLayout) variantPanel.getLayout ();
		String type = this.variantComboBox.getSelectedItem ().toString ();
		layout.show (this.variantPanel, type);
	 }//GEN-LAST:event_variantComboBoxActionPerformed

	 private void probabilityFirstPlayerActProponentFormattedTextFieldPropertyChange (java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_probabilityFirstPlayerActProponentFormattedTextFieldPropertyChange
		if (evt.getPropertyName ().equals ("value")) {
			final float value = (Float) this.probabilityFirstPlayerActProponentFormattedTextField.getValue ();
			final boolean ok = value >= 0 && value <= 1;
			this.update (this.probabilityFirstPlayerActProponentFormattedTextField, ok);
			if (ok) {
				this.error [UltimatumGame.Variant.SINGLE_GAME.ordinal ()] &= ~1;
			}
			else {
				this.error [UltimatumGame.Variant.SINGLE_GAME.ordinal ()] |= 1;
			}
		}
	 }//GEN-LAST:event_probabilityFirstPlayerActProponentFormattedTextFieldPropertyChange


   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JFormattedTextField cakeSizeFormattedTextField;
   private javax.swing.JPanel commonParametersPanel;
   private javax.swing.JFormattedTextField probabilityFirstPlayerActProponentFormattedTextField;
   private javax.swing.JPanel simultaneousPanel;
   private javax.swing.JPanel singlePanel;
   private javax.swing.JComboBox variantComboBox;
   private javax.swing.JPanel variantPanel;
   // End of variables declaration//GEN-END:variables

}

// Local Variables:
// mode: java
// mode: flyspell-prog
// ispell-local-dictionary: "british"
// End:
